import mysql
import logging
from spider.database.mysqlUtil import MysqlUtil


class MysqlPipeline:

    # 初始化，连接库
    def __init__(self):
        self.mysqlUtil = MysqlUtil()

    # def open_spider(self, spider):
        # logging.info('<============初始化数据库连接============>')
        # self.conn = mysql.connector.connect(
        #     host=spider.settings.get('MYSQL_HOST'),
        #     port=spider.settings.get('MYSQL_PORT'),
        #     user=spider.settings.get('MYSQL_USER'),
        #     password=spider.settings.get('MYSQL_PASSWORD'),
        #     database=spider.settings.get('MYSQL_DATABASE'),
        #     use_unicode=True,
        # )
        # self.cur = self.conn.cursor()

    # 处理数据，写入库
    def process_item(self, item, spider):
        # if item['page_title'] == 'Document of interest: AFED - Impact of Climate Change on the Arab Countries':
        #     logging.info('================================')
        #     logging.info(len(item['page_text_body']))

        # self.mysqlUtil.insert('''INSERT INTO `basic_news` (`page_title`, `page_url`, `page_text_body`,
        #                                  `page_publish_date`, `page_publish_timestamp`, `page_tags`, `page_lang`,
        #                                  `page_author`, `page_category`, `page_from_site`, `page_crawl_time`,
        #                                  `page_file_link`, `page_file_save_path`
        #                                  ) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
        #                                 ''',
        #                       (item['page_title'], item['page_url'], item['page_text_body'],
        #                        item['page_publish_date'], item['page_publish_timestamp'], item['page_tags'],
        #                        item['page_lang'],
        #                        item['page_author'], item['page_from_site'], item['page_crawl_time'],
        #                        item['page_crawl_time'],
        #                        item['page_file_link'], item['page_file_save_path']))

        self.mysqlUtil.insert('''INSERT INTO `basic_news` (`page_title`, `page_url`, `page_text_body`,
                                 `page_publish_date`, `page_publish_timestamp`, `page_tags`, `page_lang`,
                                 `page_author`, `page_category`, `page_from_site`, `page_crawl_time`,
                                 `page_file_link`, `page_file_save_path`
                                 ) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
                                ''',
                      (item['page_title'], item['page_url'], item['page_text_body'],
                       item['page_publish_date'], item['page_publish_timestamp'], item['page_tags'],
                       item['page_lang'],
                       item['page_author'], item['page_category'], item['page_from_site'],
                       item['page_crawl_time'],
                       item['page_file_link'], item['page_file_save_path']))

    # 结束，关闭连接
    # def close_spider(self, spider):
    #     print('关闭数据库资源')
    #     # 关闭游标
    #     self.cur.close()
    #     # 关闭连接
    #     self.conn.close()
